<?php
namespace App\Repos;

use App\Models\ConsultReply;
use App\Models\Consult;
use App\Services\Wechat;
use Log;

class ConsultReplyRepo 
{
    public static function save($data)
    {
    	$consult = Consult::find($data['consult_id']);
        $consult->status = $data['status'];
        $consult->save();
        array_pull($data,'status');
        $flag    = ConsultReply::create($data);
    	

    	if($flag) {
            $msg = $data;  
    		$msg['code'] = $consult->code;
            $msg['title']= $consult->title;
            $msg['url']    = config('app.url').'/user/consult-show/'.$data['consult_id'];
            if($data['type'] == 1) {                       
                $msg['openid'] = $consult->wxuser_openid;   
                $msg['status'] = 1;      //状态改为已回复     
                $msg['name']   = object_get($consult,'expert.user.truename');
                

                // 专家的回复要推送给用户
                Wechat::sendMsgTpl('reply',$msg);
                Log::info('回复：推送给用户成功');    
            } else {
                // 用户的追问推送给专家                
                $msg2 = $msg;     
                $msg2['name']   = $consult->truename;
                $msg2['openid'] = $consult->expert_openid;
                Wechat::sendMsgTpl('reply',$msg2);
                Log::info('追问：推送给专家成功');    
            }
            
            return $flag;
              
        	
    	}
    	
    }
}
